package com.hwadee.property.controller.house;

import com.hwadee.property.pojo.Building;
import com.hwadee.property.service.IBuildingService;
import com.hwadee.property.util.AjaxResult;
import com.hwadee.property.dto.PageResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/buildings")
@Api(tags = "楼栋-BuildingController")
@CrossOrigin
public class BuildingController {

    @Autowired
    private IBuildingService buildingService;

    /**
     * 【管理员】分页查询楼栋信息
     *
     * @param token         访问令牌，需包含在请求头中
     * @param current       当前页码，默认为 1
     * @param size          每页条数，默认为 10
     * @param communityId   小区 ID（可选）
     * @param buildingNumber 楼栋号（可选）
     * @param buildingUnit  单元号（可选）
     * @param floorCount    楼层数（可选）
     * @param households    住户数（可选）
     * @param location      位置描述（可选）
     * @param houseType     房屋类型（可选）
     * @return 分页后的楼栋信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "【管理员】分页查询楼栋信息", notes = "支持按小区 ID 过滤，并可筛选楼栋相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "访问令牌", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "communityId", value = "小区 ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingNumber", value = "楼栋号", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingUnit", value = "单元号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "floorCount", value = "楼层数", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "households", value = "住户数", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "location", value = "位置描述", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "houseType", value = "房屋类型", required = false, dataType = "string", paramType = "query")
    })
    public PageResult<Building> getPage(
            @RequestHeader("Authorization") String token,
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "communityId", required = false) Long communityId,
            @RequestParam(value = "buildingNumber", required = false) Long buildingNumber,
            @RequestParam(value = "buildingUnit", required = false) Integer buildingUnit,
            @RequestParam(value = "floorCount", required = false) Integer floorCount,
            @RequestParam(value = "households", required = false) Integer households,
            @RequestParam(value = "location", required = false) String location,
            @RequestParam(value = "houseType", required = false) String houseType) {

        return buildingService.getPage(current, size, communityId, buildingNumber, buildingUnit, floorCount, households, location, houseType);
    }

    @PostMapping
    @ApiOperation(value = "【管理员】添加楼栋信息", notes = "添加新的楼栋信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "小区ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingNumber", value = "楼栋号", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingUnit", value = "楼栋单元号", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "floorCount", value = "楼层数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "location", value = "楼栋位置", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "houseType", value = "房屋类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "households", value = "住户数量", required = true, dataType = "int", paramType = "query")
    })
    public AjaxResult addBuilding(@RequestHeader("Authorization") String token,
                                  @RequestParam(required = true) Long communityId,
                                  @RequestParam(required = true) Long buildingNumber,
                                  @RequestParam(required = true) Integer buildingUnit,
                                  @RequestParam(required = true) Integer floorCount,
                                  @RequestParam(required = true) String location,
                                  @RequestParam(required = true) String houseType,
                                  @RequestParam(required = true) Integer households) {
        Building building = new Building();
        building.setCommunityId(communityId);
        building.setBuildingNumber(buildingNumber);
        building.setBuildingUnit(buildingUnit);
        building.setFloorCount(floorCount);
        building.setLocation(location);
        building.setHouseType(houseType);
        building.setHouseholds(households);
        boolean success = buildingService.addBuilding(building);
        if (success) {
            return AjaxResult.success(building);
        } else {
            return AjaxResult.fail("楼栋添加失败");
        }
    }

    @PutMapping("/{buildingId}")
    @ApiOperation(value = "【管理员】更新楼栋信息", notes = "根据楼栋 ID 更新楼栋信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingId", value = "楼栋ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "communityId", value = "小区ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingNumber", value = "楼栋号", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "buildingUnit", value = "楼栋单元号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "floorCount", value = "楼层数量", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "location", value = "楼栋位置", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "houseType", value = "房屋类型", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "households", value = "住户数量", required = false, dataType = "int", paramType = "query")
    })
    public AjaxResult updateBuilding(@RequestHeader("Authorization") String token,
                                     @PathVariable Long buildingId,
                                     @RequestParam(required = false) Long communityId,
                                     @RequestParam(required = false) Long buildingNumber,
                                     @RequestParam(required = false) Integer buildingUnit,
                                     @RequestParam(required = false) Integer floorCount,
                                     @RequestParam(required = false) String location,
                                     @RequestParam(required = false) String houseType,
                                     @RequestParam(required = false) Integer households) {
        Building building = new Building();
        building.setId(buildingId);
        if (communityId != null) building.setCommunityId(communityId);
        if (buildingNumber != null) building.setBuildingNumber(buildingNumber);
        if (buildingUnit != null) building.setBuildingUnit(buildingUnit);
        if (floorCount != null) building.setFloorCount(floorCount);
        if (location != null) building.setLocation(location);
        if (houseType != null) building.setHouseType(houseType);
        if (households != null) building.setHouseholds(households);
        boolean success = buildingService.updateBuilding(building);
        if (success) {
            return AjaxResult.success(building);
        } else {
            return AjaxResult.fail("楼栋更新失败");
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "【管理员】删除楼栋信息", notes = "根据楼栋 ID 删除楼栋信息")
    public AjaxResult deleteBuilding(@RequestHeader("Authorization") String token, @PathVariable Long id) {
        boolean success = buildingService.deleteBuilding(id);
        if (success) {
            return AjaxResult.success("楼栋删除成功");
        } else {
            return AjaxResult.fail("楼栋删除失败");
        }
    }
}